package mod.luckymod.util;

import mod.luckymod.ModEntities;
import mod.luckymod.entity.SodiumEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;


public class LaunchEntity {
    public static void LanuchEntity(BlockPos startpos, Level level, int count) {
        RandomSource random = RandomSource.create();
        int shardCount = count + random.nextInt(3);

        for (int i = 0; i < shardCount; i++) {
            // 每次循环都创建新实体
            if (level instanceof ServerLevel) {
                SodiumEntity entity = new SodiumEntity(ModEntities.SodiumEntity.get(), level);
                // 设置精确位置（方块中心）
                entity.create(level, startpos);
                // 随机初速度（模拟飞溅效果）
                double speed = 0.5 + random.nextDouble() * 0.7;
                double angle = random.nextDouble() * Math.PI * 2;
                double pitch = random.nextDouble() * Math.PI / 4; // 0-45度
                Vec3 velocity = new Vec3(
                        Math.sin(angle) * Math.cos(pitch) * speed,
                        Math.sin(pitch) * speed + 0.2, // 稍微向上
                        Math.cos(angle) * Math.cos(pitch) * speed
                );
                entity.setDeltaMovement(velocity);
                level.addFreshEntity(entity);
            }
        }
    }
}